class LinkedSrack:
    """单向链表实现栈"""

    # ---------------------栈类---------------------
    class _Node:
        __slots__ = 'element', 'next'

        def __init__(self, element, next_):
            self.element = element
            self.next = next_

    # ---------------------栈类---------------------

    def __init__(self):
        self.head = None
        self.size = 0

    def len(self):
        return self.size

    def is_empty(self):
        return self.size == 0

    def push(self, data):
        """Add a data to 栈顶"""
        self.head = self._Node(data, self.head)
        self.size += 1

    def top(self):
        if self.is_empty():
            raise Exception('栈无内容')
        return self.head.element

    def pop(self):
        if self.is_empty():
            raise Exception('栈无内容')
        data = self.head.element
        self.head = self.head.next
        self.size -= 1
        return data

    def __str__(self):
        data = []
        current = self.head
        while current is not None:
            data.append(current.element)
            current = current.next
        return str(data)


if __name__ == '__main__':
    link1 = LinkedSrack()
    for i in range(10):
        link1.push(i)
    print(link1)

    link2 = LinkedSrack()
    for i in range(9,-1,-1):
        link2.push(i)
    print(link2)

    def find(link,index):
        i = 1
        current = link.head
        while i < index:
            current = current.next
            i += 1
        return current.element

    def links(link1,link2):
        i = 1
        new_head = link1.head
        while i < link1.size:
            link1.head = link1.head.next
            i += 1
        link1.head.next = link2.head
        link1.head = new_head
        link1.size += link2.size

    # links(link1,link2)
    # print(link1)
    # print(link2)

